//
//  CYCreditDetailWebVC.m
//  CreditAddressBook
//
//  Created by Mosu&Cuu on 16/4/18.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYCreditDetailWebVC.h"
#import "CYUser.h"
#import "CYLoginHttpTool.h"
#import "UIAlertView+Block.h"
#import "MBProgressHUD+LE.h"

@interface CYCreditDetailWebVC ()<UIWebViewDelegate>

@property (nonatomic, strong)UIWebView *webview;
@property (nonatomic, strong)UIView *hiview;
@property (nonatomic, assign)BOOL isfirstLoad;
@property (nonatomic, strong)NSString *url1;

@end

@implementation CYCreditDetailWebVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    self.title  = @"信用明细";
    //[self.navigationBar setBackgroundColor:[UIColor blackColor]];
    CYUser *user = [CYLoginHttpTool userData].user;
    self.view.backgroundColor = [UIColor blackColor];
    _webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen ]bounds].size.width,[[UIScreen mainScreen ]bounds].size.height)];
    _webview.backgroundColor = CYColor(68, 68, 68, 1);
    _webview.delegate = self;
    
    //测试：信用明细  http://192.168.0.199:8080/creditloan/creditDetail.htm
    //http://www.credit-manage.com:8058/creditloan/creditDetail.htm   http://192.168.0.254:8058/creditloan/creditDetail.htm
    if (user) {
        NSString *url= [NSString stringWithFormat:@"http://www.credit-manage.com:8058/creditloan/creditDetail.htm?&userId=%@&token=%@",user.userId,user.token];
        NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:url]];
        [_webview loadRequest:request];
    }     
    //添加滑动返回
    UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
    rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [_webview addGestureRecognizer:rightSwipeGestureRecognizer];
    // Do any additional setup after loading the view.
    [self.view addSubview:_webview];
    self.isfirstLoad = YES;

    // Do any additional setup after loading the view from its nib.
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
//     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
//     [alert showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
//          if (buttonIndex == 0) {
//               [[self navigationController] setNavigationBarHidden:NO animated:NO];
//               [self.navigationController popViewControllerAnimated:YES];
//               
//          }
//     }];
     
}

/**
 *  添加滑动返回
 */
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender {
    if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
        [[self navigationController] setNavigationBarHidden:NO animated:NO];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    [self.view addSubview:_hiview];
    NSString *url = [[request URL] absoluteString];
    NSLog(@"url = %@",url);
     if([url rangeOfString:@"report"].location !=NSNotFound)//_roaldSearchText
     {
          NSString *str = url;
          CYLog(@"str = %@",str);
          NSString *str1 = [str substringFromIndex:12];//截取掉下标11之后的字符串
          CYLog(@"str1 = %@",str1);
          if (str1 && !str1.isEmpty) {
               
               UIPasteboard *pab = [UIPasteboard generalPasteboard];
               [pab setString:str1];
               if (pab == nil) {
                    [MBProgressHUD showError:@"复制失败"];
                    
               }else
               {
                    [MBProgressHUD showSuccess:@"已复制"];
                    
               }
               
               
          }
     }
     else
     {
          NSLog(@"no");
     }
    //h5内返回控件字段方法:loanlist
    if([url hasSuffix:@"loanlist"]) {
        dispatch_async(dispatch_get_main_queue(), ^{
            //返回上一级时，取消nav的隐藏
            [[self navigationController] setNavigationBarHidden:NO animated:NO];
            //js交互oc获取到h5内的返回按钮后，点击返回，popvc
            [self.navigationController popViewControllerAnimated:YES];
        });
        return NO;
    }
    
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
